package com.example.tests;

import com.thoughtworks.xstream.XStream;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

import static com.example.fw.RandomHelper.*;

/**
 * Created with IntelliJ IDEA.
 * User: pshevchenko
 * Date: 28.03.13
 * Time: 11:32
 * To change this template use File | Settings | File Templates.
 */
public class AddressDataGenerator {

    private static void saveAddressesToXML(List<AddressData> addressDataList, File outputFile) throws IOException{
        XStream xStream = new XStream();
        xStream.alias("address", AddressData.class);
        String xmlString = xStream.toXML(addressDataList);
        FileWriter fileWriter = new FileWriter(outputFile);
        fileWriter.write(xmlString);
        fileWriter.close();

    }

    private static void saveAddressesToCSV(List<AddressData> addressDataList, File outputFile) throws IOException{
        FileWriter fileWriter = new FileWriter(outputFile);

        for (AddressData addressData : addressDataList)
        {
            fileWriter.write(
                    addressData.getFirstName() + ","
                            + addressData.getLastName() + ","
                            + addressData.getAddressString() + ","
                            + addressData.getHomePhone() + ","
                            + addressData.getCellPhone() + ","
                            + addressData.getWorkPhone() + ","
                            + addressData.geteMail1() + ","
                            + addressData.geteMail2() + ","
                            + addressData.getBirthDay() + ","
                            + addressData.getBirthMonth() + ","
                            + addressData.getBirthYear() + ","
                            + addressData.getGroupName() + ","
                            + addressData.getAddressString2() + ","
                            + addressData.getHomePhone2() + "\n"
            );
        }

        fileWriter.close();
    }

    public static void main(String[] args) throws IOException{

        if (args.length < 3){
            System.out.println("Please specify parameters: <amount of test data> <file> <format>");
            return;
        }

        int amount = Integer.parseInt(args[0]);
        File outputFile = new File(args[1]);
        String format = args[2];

        if (outputFile.exists()){
            System.out.println(outputFile + " already exists");
            return;
        }

        List<AddressData> addressDataList = generateRandomAddresses(amount);

        if ("csv".equals(format)){
            saveAddressesToCSV(addressDataList, outputFile);
        }
        else if ("xml".equals(format)){
            saveAddressesToXML(addressDataList, outputFile);
        }
        else {
            System.out.println("unsupported file format " + format);
        }

    }

    public static List<AddressData> loadAddressesFromXML(File inputFile) throws IOException{
        XStream xStream = new XStream();
        xStream.alias("address", AddressData.class);
        return (List<AddressData>)xStream.fromXML(inputFile);
    }

    public static List<AddressData> loadAddressesFromCSV(File inputFile) throws IOException{
        List<AddressData> resultList = new ArrayList<AddressData>();
        FileReader fileReader = new FileReader(inputFile);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String nextLine = bufferedReader.readLine();
        while (nextLine != null){
            String[] splitLine = (nextLine + ",-").split(",");
            resultList.add(
                    new AddressData()
                            .withFirstName(splitLine[0])
                            .withLastName(splitLine[1])
                            .withAddressString(splitLine[2])
                            .withHomePhone(splitLine[3])
                            .withCellPhone(splitLine[4])
                            .withWorkPhone(splitLine[5])
                            .withEMail1(splitLine[6])
                            .withEMail2(splitLine[7])
                            .withBirthDay(splitLine[8])
                            .withBirthMonth(splitLine[9])
                            .withBirthYear(splitLine[10])
                            .withGroupName(splitLine[11])
                            .withAddressString2(splitLine[12])
                            .withHomePhone2(splitLine[13])
            );
            nextLine = bufferedReader.readLine();
        }
        bufferedReader.close();
        return resultList;
    }

    public static List<AddressData> generateRandomAddresses(int iterationsCount) {

        List<AddressData> result = new ArrayList<AddressData>();
        int randomStringMaxLength = 15;
        int nullFreq = 5;
        for (int i = 0; i < iterationsCount; i++){
            AddressData addressData = new AddressData()
                    .withFirstName(getNullable(getRandomStringValue(randomStringMaxLength), nullFreq))
                    .withLastName(getNullable(getRandomStringValue(randomStringMaxLength), nullFreq))
                    .withAddressString(getNullable(getRandomStringValue(randomStringMaxLength), nullFreq))
                    .withAddressString2(getNullable(getRandomStringValue(randomStringMaxLength), nullFreq))
                    .withHomePhone(getNullable(getRandomStringValue(randomStringMaxLength), nullFreq))
                    .withCellPhone(getNullable(getRandomStringValue(randomStringMaxLength), nullFreq))
                    .withWorkPhone(getNullable(getRandomStringValue(randomStringMaxLength), nullFreq))
                    .withEMail1(getNullable(getRandomStringValue(randomStringMaxLength), nullFreq))
                    .withEMail2(getNullable(getRandomStringValue(randomStringMaxLength), nullFreq))
                    .withHomePhone2(getNullable(getRandomStringValue(randomStringMaxLength), nullFreq))
                    .withBirthDay(getNullable(getNextIntDropdown(31), nullFreq))
                    .withBirthMonth(getNullable(getMonthList().get(getRnd().nextInt(getMonthList().size())), nullFreq))
                    .withBirthYear(getNullable("" + getRnd().nextInt(10000), nullFreq));

            result.add(addressData);
        }
        return result;
    }
}